import { defineStore } from 'pinia'

export const useAppStore = defineStore('app', {
  state: () => ({
    // 侧边栏状态
    sidebar: {
      opened: true,
      withoutAnimation: false
    },
    // 设备类型
    device: 'desktop',
    // 主题
    theme: 'light',
    // 语言
    language: 'zh-cn'
  }),

  getters: {
    isMobile: (state) => state.device === 'mobile',
    isDesktop: (state) => state.device === 'desktop'
  },

  actions: {
    // 切换侧边栏
    toggleSidebar() {
      this.sidebar.opened = !this.sidebar.opened
      this.sidebar.withoutAnimation = false
    },

    // 关闭侧边栏
    closeSidebar(withoutAnimation = false) {
      this.sidebar.opened = false
      this.sidebar.withoutAnimation = withoutAnimation
    },

    // 设置设备类型
    setDevice(device) {
      this.device = device
      // 移动端自动关闭侧边栏
      if (device === 'mobile') {
        this.closeSidebar(true)
      }
    },

    // 设置主题
    setTheme(theme) {
      this.theme = theme
    },

    // 设置语言
    setLanguage(language) {
      this.language = language
    }
  },

  persist: {
    key: 'app-store',
    storage: localStorage,
    paths: ['theme', 'language', 'sidebar.opened']
  }
})
